/*
 * Copyright 2019-2029 geekidea(https://github.com/geekidea)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.app.brilliant.shiro.controller;
import com.app.brilliant.common.api.ApiResult;
import com.app.brilliant.game.service.WxConfigService;
import com.app.brilliant.shiro.param.LoginParam;
import com.app.brilliant.shiro.param.SystemRegisterParam;
import com.app.brilliant.shiro.service.LoginService;
import com.app.brilliant.shiro.util.JwtTokenUtil;
import com.app.brilliant.shiro.vo.sys.LoginSysUserTokenVo;
import com.app.brilliant.sign.constant.Constants;
import com.app.brilliant.sys.service.SysUserService;
import com.app.brilliant.util.CookiesUtil;
import com.app.brilliant.util.IpUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
/**
 * 登陆控制器
 *
 * @author geekidea
 * @date 2019-09-28
 * @since 1.3.0.RELEASE
 **/
@Slf4j
@RestController
@ApiIgnore
@RequestMapping("/sys")
public class SysLoginController {

    @Autowired
    private LoginService loginService;
    
    @Autowired
    private WxConfigService wxConfigService;

    @Autowired
    private SysUserService sysUserService;
    @PostMapping("/login")
    @ApiOperation(value = "登陆", notes = "系统用户登陆", response = LoginSysUserTokenVo.class)
    public ApiResult<LoginSysUserTokenVo> login(@CookieValue(value = "verifyToken", required = false) String verifyToken, @Valid LoginParam loginParam, HttpServletResponse response) throws Exception {
        LoginSysUserTokenVo loginSysUserTokenVo = loginService.systemLogin(loginParam, verifyToken);
        // 设置token响应头
        response.setHeader(JwtTokenUtil.getTokenName(), loginSysUserTokenVo.getToken());
        CookiesUtil.setCookie(response, JwtTokenUtil.getTokenName(), loginSysUserTokenVo.getToken(), -1);
        return ApiResult.ok(loginSysUserTokenVo, "登陆成功");
    }

    @PostMapping("/register")
    @ApiOperation(value = "系统用户注册", notes = "系统用户注册")
    public ApiResult<String> register(@Valid SystemRegisterParam systemRegisterParam, HttpServletRequest request) throws Exception {
        // 从wx_config中查询systemRegisterIp
        String systemRegisterIp = wxConfigService.getConfigValue(Constants.NomalConfigKey.systemRegisterIp);
        if (systemRegisterIp == null) {
            return ApiResult.fail("系统未配置允许注册的IP地址");
        }
        // 获取请求IP
        String requestIp = IpUtil.getRequestIp();
        
        // 判断IP是否一致
        if (!systemRegisterIp.equals(requestIp)) {
            return ApiResult.fail("IP地址不匹配，禁止注册");
        }
        // 调用注册逻辑
        sysUserService.register(systemRegisterParam);
        return ApiResult.ok("注册成功");
    }
    @PostMapping("/logout")
    public ApiResult logout(HttpServletRequest request, HttpServletResponse response) throws Exception {
        loginService.logoutSys(request);
        CookiesUtil.setCookie(response, "token", "", 0);
        return ApiResult.ok("退出成功");
    }

    @GetMapping("/getIp")
    public ApiResult<String> getRequestIp(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String requestIp = IpUtil.getRequestIp();
        return ApiResult.ok(requestIp);
    }
}
